#include "postgresql.hpp"

// Returns false if the supplied string matches a signed number
// (but not with an explicit '+', I can't remember why plus had
// to be excluded out, sorry) possibly with decimals, or a date
// with symbols '-', '.' or '/' as separators, or an ip address
// lookalike (i.e. anything which can match the regex below).
bool is_word( const string& str ){

	// The naming in this case is very "poor" since dates (and
	// (other funny - though surely "non-word" - possibilities
	// which arise from the regex) are also recognized here!
	static const string sign = "(-|\\*|/)?";
	static const string decis = "((\\.|-)\\d*)*";
	static const RE num_or_date( sign+"\\d*"+decis );
	if( num_or_date.FullMatch( str ) )
		return false;

	return true;

}

// Needs to consider matching quotes, all types of "brackets", etc.
vector<string>& tokenize( const string& s, const bool& filter, const bool& blurt,
		const char delimiters[] = " \t,;(<>)+^=/\"|*'[]" ){

	vector<string> *tokens = new vector<string>;

	static const regex curly1( "(\\{\\s*)+" );
	static const regex curly2( "(\\s*\\})+" );
	string s2 = regex_replace( s, curly1, "{" );
	s2 = regex_replace( s2, curly2, "}" );

	char *str = new char[ s2.length()+1 ]; // Why >=1(0)?
	strcpy( str, s2.c_str() );

	char * word;
	word = strtok( str, delimiters );
	while( word != NULL ){
		string str = word;
		if( not filter )
			tokens->push_back( str );
		else if( is_word( str ) ){
			if( is_not_keyword( str ) ){
				if( blurt ) cout << str << endl;
				tokens->push_back( str );
			}
		}
		word = strtok( NULL, delimiters );
	}
	return *tokens;

}
